# Copyright (C) 2001, 2003-2004, 2008-2009 The Written Word, Inc.
# Copyright (C) 2001 The Written Word, LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: depotdb.py 694 2010-02-19 14:55:45Z china $

import os
import string
import sys

from xmlutil import XMLDTDError, XMLParseError, XMLSyntaxError
from xmlutil import DTD, dtd_search, objectify
from version import Version

class DepotDBEntry:
  def __init__ (self, element):
    self.auto_include = int (element.get ('auto-include', '1'))
    self.category = element.get ('category')
    self.dist_ver = element.get ('dist-ver')
    self.name = element.get ('name')
    self.package = element.get ('package')
    self.program = element.get ('program')
    self.version = Version (element.get ('version', '0'))
    self.revision = Version (element.get ('revision', '0'))
    self.systype = element.get ('systype', element.get ('arch'))

    # backward compatibility with 'package' attribute
    if not self.package:
      if self.program and self.version and self.revision:
        self.package = self.program + '-' + \
                       str (self.version) + '-' + \
                       str (self.revision)
      elif self.program and self.version:
        self.package = self.program + '-' + str (self.version)
      elif self.program:
        self.package = self.program

  def __str__ (self):
    return '<DepotDBEntry name="%s" package="%s" program="%s" version="%s" dist_ver="%s">' % (self.name, self.package, self.program, self.version, self.dist_ver)

# read depot-db.xml file
def read_depotdb_xml (file, dep):
  depotdb_path = os.path.join (dep.local_path, file)

  try:
    xml = objectify.parse (depotdb_path)
  except XMLSyntaxError, msg:
    raise XMLParseError ('Syntax error while parsing ' + depotdb_path +
                         ', the following error was encountered: ' +
                         str (msg))

  # Find a DTD, and use it to validate depot-db.xml
  dtd = dtd_search (dep.dtd_dirs, depotdb_path, 'depot-db.dtd')  
  if dtd:
    depotdtd = DTD (dtd)
    if not depotdtd.validate (xml):
      raise XMLDTDError ('DTD validation error while parsing ' +
                         depotdb_path + 
                         ', the following error was encountered: ' +
                         depotdtd.error_log().filter_from_errors()[0].message)

  # return a list of DepotDBEntry objects
  return [DepotDBEntry (sub_depot) for sub_depot in xml.getroot().depot]
